﻿//=============================================================
// Copyright (C) 2015 福流网络
// 作 者：江怀玉
// 日 期：2015-05-11
// 描 述: 通用的数据视图[MINIUI]
// URL Parameter：
//        entity:对应的实体
//        condition:过滤数据条件
//        order:排序字段
// 版 本：1.00
// 修改历史纪录
// 版 本  修改时间      修改人            修改内容
// 1.00   2015-05-11   江怀玉           
//=============================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.DBModule;
using ZX.EHR.Library;
using ZX.BLL.Extend;

namespace ZX.Web.UI.Controls
{
    public partial class miniDataGrid : miniListPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //// 权限 
            //if (this.UserId == null)
            //{
            //    Response.Redirect("/Error.aspx?message=1&functionId=" + FunctionID);
            //}
            //if (!CheckFunctionRight())
            //{
            //    Response.Redirect("/Error.aspx?message=2&functionId=" + FunctionID);
            //}
            SetButtons();
        }

        public override void SetButtons()
        {
            //base.SetButtons();
            string functionid = GetStringFromQueryString("functionid", "");
            V_FunctionRight function = (new BaseBussnies(new V_FunctionRight())).GetEntityByCondition("FunctionID='" + functionid + "'") as V_FunctionRight;
            string userID = this.UserId.ToString();
            if (function == null)
            {
                BtnItems = "";
                BtnEvents = "";
                InitBtnEvents = "";
            }
            else
            {
                string[] btns = function.Opration.Split(',');
                string[] btnTexts = function.BarItems.Split(',');
                string[] fns = function.FnItems.Split(',');
                string[] barItems = (new BLL.BUser()).GetFunctions(userID, functionid)[0].Split(',').Distinct().ToArray();
                if (barItems == null)
                {
                    BtnItems = "";
                    BtnEvents = "";
                    InitBtnEvents = "";
                    return;
                }
                if (barItems.Count() == 0)
                {
                    BtnItems = "";
                    BtnEvents = "";
                    InitBtnEvents = "";
                    return;
                }
                //生成按钮
                StringBuilder strHTML = new StringBuilder("");
                StringBuilder strHTML2 = new StringBuilder("");
                int index = 0;
                for (int i = 0; i < btns.Length; i++)
                {
                    index = index + 1;

                    if (barItems.Contains(btns[i]) || this.UserName.ToLower() == "admin")
                    {
                        string strFormater = "<a class=\"mini-button\"  iconcls=\"{0}\" plain=\"true\"  onclick=\"{1}\" id=\"{3}\">{2}</a>";
                        strHTML.Append(string.Format(strFormater, "", fns[i], btnTexts[i], btns[i]));

                    }
                }
                BtnItems = strHTML.ToString();
            }
        }

        public override string EntityId
        {

            get
            {
                BaseEntity instance = ZX.Common.Reflection.CreateEntitysInstance(this.EntityName) as BaseEntity;
                if (instance != null)
                {
                    return instance.GetTableId();
                }
                DataTableBaseEntity entity = new DataTableBaseEntity();
                entity.TableName = this.EntityName;
                entity.TableKey = entity.GetTableId();
                return entity.TableKey;
            }
        }


        public override string GeneratorQueryItem()
        {
            if (list == null)
            {
                _bll.GetEntitys<t_set_datagrid>("function_id='" + FunctionID + "'");
            }
            StringBuilder strHTML = new StringBuilder("");
            string strDiv1 = "<div class=\"col1\" id=\"query_item_{0}\"><table class=\"query_table\"><tr><td class=\"label\">{1}</td><td class=\"exp\">{2}</td><td>{3}</td></tr></table></div>";
            string strDiv2 = "<div class=\"col2\" id=\"query_item_{0}\"><table class=\"query_table\"><tr><td class=\"label\">{1}</td><td class=\"exp\">{2}</td><td>{3}</td><td>{4}</td></tr></table></div>";

            var _list = list.OrderBy(p => p.sort);
            foreach (t_set_datagrid entity in _list)
            {
                if (entity.is_define_query == "N")
                {
                    continue;
                }
                if (entity.is_visiable == "N")
                {
                    continue;
                }
                if (entity.data_type.ToLower().Contains("date"))
                {
                    strHTML.AppendLine(string.Format(strDiv2, entity.name, entity.caption, GetExp(entity), CovertCtrl(entity), CovertCtrl(entity, 0)));
                }
                else if (entity.data_type.ToLower().Contains("int"))
                {
                    if (entity.is_between == "Y")
                    {
                        strHTML.AppendLine(string.Format(strDiv2, entity.name, entity.caption, GetExp(entity), CovertCtrl(entity), CovertCtrl(entity, 0)));
                    }
                    else
                    {
                        strHTML.AppendLine(string.Format(strDiv1, entity.name, entity.caption, GetExp(entity), CovertCtrl(entity)));
                    }
                }
                else
                {
                    strHTML.AppendLine(string.Format(strDiv1, entity.name, entity.caption, GetExp(entity), CovertCtrl(entity)));
                }
            }
            return strHTML.ToString();
        }

        public override string GetExp(t_set_datagrid entity)
        {
            string str = "";
            if (entity.data_type.Contains("char"))
            {
                str = "<select><option value=\"like\">包含</option><option value=\"=\">等于</option><option value=\" not like\">不包含</option> <option value=\"<>\">不等于</option> </select>";

            }
            else
            {
                str = "<select><option value=\"=\">等于</option><option value=\">\">大于</option><option value=\"<\">小于</option><option value=\"between\">在..之间</option></select>";
            }
            return str;
        }



        public string Order
        {
            get
            {
               return  GetStringFromQueryString("order", "");
            }
        }
        /// <summary>
        /// 生成数据列头和数据对象
        /// </summary>
        /// <returns></returns>
        public override string CreateDataGridColumns()
        {
            
            //if (list == null)
          
                t_set_datagrid template = new t_set_datagrid();
                BaseBussnies<t_set_datagrid> bll = new BaseBussnies<t_set_datagrid>(template);
             var _new_list = bll.GetEntityListByCondition<t_set_datagrid>(" entity='" + EntityName + "' and is_visiable<>'N'");
            
            string _strHtml = "key:'{0}',label:'{1}',width:{2},{3}";
            if (list == null)
            {
                return ("{" + string.Format(_strHtml, "Not Data", "Not Data", 400, "formatter:\"\"") + "},");
            }
            StringBuilder _sbHtml = new StringBuilder("");
            var _list = _new_list.Where(p => p.is_visiable == "Y").OrderBy(p => p.sort).ToList();
            int i = 0;
            string _header = "";
            foreach (t_set_datagrid _template in _list)
            {
                i = i + 1;
                if (_header != _template.header)
                {
                    if (!_template.is_created&&!string.IsNullOrEmpty(_template.header))
                    {
                        _header = _template.header;
                        var _tlist = _list.Where(p => p.header == _header);
                        _sbHtml.Append(string.Format("<div header=\"{0}\"><div property=\"columns\">", _header));
                        foreach (t_set_datagrid _t in _tlist)
                        {
                            _t.is_created = true;
                            if (string.IsNullOrEmpty(_t.column_type))
                            {
                                if (AllowEdit)
                                {
                                    _sbHtml.Append(string.Format("<div field=\"{0}\" width=\"{1}\" headerAlign=\"center\" allowSort=\"true\" renderer=\"{5}\">{2}<input property=\"editor\" class=\"mini-{4}\" style=\"width:100%;\" {3} /></div>\n",
                                        _t.name,
                                        _t.width,
                                        _t.caption,
                                        (!string.IsNullOrEmpty(_t.data_source_type) && !string.IsNullOrEmpty(_t.data_source) ? string.Format(" data=\"{0}_data\"", _t.name) : ""),
                                        string.IsNullOrEmpty(_t.editor) ? "textbox" : _t.editor, _t.data_formatter)
                                      );
                                }
                                else
                                {
                                    _sbHtml.Append(string.Format("<div field=\"{0}\" width=\"{1}\" headerAlign=\"center\" allowSort=\"true\"  renderer=\"{3}\" >{2}</div>\n", _t.name, _t.width, _t.caption, _t.data_formatter));
                                }
                            }
                            else
                            {
                                _sbHtml.Append(string.Format("<div field=\"{0}\" width=\"{1}\" headerAlign=\"center\" allowSort=\"true\" type=\"{3}\"  renderer=\"{5}\" >{2}<input property=\"editor\" class=\"mini-{4}\" style=\"width:100%;\" data=\"{0}_data\" /></div>\n", _t.name, _t.width, _t.caption, _t.column_type, string.IsNullOrEmpty(_t.editor) ? _t.column_type.Replace("column", "") : _t.editor, _t.data_formatter));
                            }
                        }
                        _sbHtml.Append("</div></div>");
                    }
                   
                }
                if (_template.is_created)
                {
                    continue;
                }
                //不相等，但不为空
                if (_header == _template.header & !string.IsNullOrEmpty(_header))
                {
                    continue;
                }
                if (string.IsNullOrEmpty(_template.column_type))
                {
                    if (AllowEdit)
                    {
                        _sbHtml.Append(string.Format("<div field=\"{0}\" width=\"{1}\" headerAlign=\"center\" allowSort=\"true\">{2}<input property=\"editor\" class=\"mini-{4}\" style=\"width:100%;\" {3}   renderer=\"{5}\"/></div>\n",
                            _template.name,
                            _template.width,
                            _template.caption,
                            (!string.IsNullOrEmpty(_template.data_source_type) && !string.IsNullOrEmpty(_template.data_source) ? string.Format(" data=\"{0}_data\"", _template.name) : ""),
                            string.IsNullOrEmpty(_template.editor) ? "textbox" : _template.editor,_template.data_formatter));
                    }
                    else
                    {
                        _sbHtml.Append(string.Format("<div field=\"{0}\" width=\"{1}\"  headerAlign=\"center\" allowSort=\"true\"  renderer=\"{3}\">{2}</div>\n", _template.name, _template.width, _template.caption,_template.data_formatter));
                    }
                }
                else
                {
                    _sbHtml.Append(string.Format("<div field=\"{0}\" width=\"{1}\" headerAlign=\"center\" allowSort=\"true\" type=\"{3}\">{2} <input property=\"editor\" class=\"mini-{4}\" style=\"width:100%;\" data=\"{0}_data\"  renderer=\"{5}\"/></div>\n", _template.name, _template.width, _template.caption, _template.column_type, _template.column_type.Replace("column", ""),_template.data_formatter));

                }
            }
            //_sbHtml.Remove(_sbHtml.Length - 1, 1);
            return (_sbHtml.ToString());
        }
    }
}